<?php 
Class wxmenuController extends appController
{
	
	public function index()
	{
		$wx=new weixinController();
		$wxmu=Model('wxmenu');
		$app=$wxmu->get_app();
		
		
		
	$token=$wxmu->getAccessToken($app['appid'], $app['appsecret']);
	$wxmenu=$wx->get_menu($token);
	$menu=(array)json_decode($wxmenu);
	
	if($menu['errcode']>0)
	{
		echo $menu['errmsg'];
	}
	
	$menu=(array)$menu['menu'];
	$menu=(array)$menu['button'];
	$count=0;
	$new_menu=array();
	
	foreach($menu as $k=>$v)
	{
		$menu[$k]=(array)$v;
		
		if($v->sub_button)
		{
		    foreach($v->sub_button as $kk=>$vv)
		   {
			$menu[$k]['sub_button'][$kk]=(array)$vv;
			$menu[$k]['sub_button'][$kk]['key']=str_replace('event_', '', $menu[$k]['sub_button'][$kk]['key']);
			
		    }
		}
		$menu[$k]['key']=str_replace('event_', '', $menu[$k]['key']);
		
	}
	
	if(!isset($_SESSION['wx_button']))
	{
		$_SESSION['wx_button']=$menu;
	}
	else 
	{
		$local_menu=$wxmu->getCfg('wxmenu');
		$editing=$this->format_menu($_SESSION['wx_button']);
	/*	echo $editing;
		echo "<br>";
		echo $local_menu;
		echo "<br>";
		echo $wxmenu;*/
		if($editing!=$local_menu)
		{
			$this->assign('unsaved',  1);
		}
		
		if($editing!=$this->format_menu($menu))
		{
			$this->assign('uncommitted',  1);
		}
	}
	
	if(isset($_SESSION['wx_button']))
	{
		$main_nums=count($_SESSION['wx_button']);
		//整理键值
		foreach($_SESSION['wx_button'] as $k=>$v)
		{
	
			if($v['sub_button'])
			{
				unset($_SESSION['wx_button'][$k]['sub_button']);
				foreach($v['sub_button'] as $kk=>$vv)
				{
	
	
					$_SESSION['wx_button'][$k]['sub_button'][$main_nums]=$vv;
					$main_nums++;
				}
			}
	
		}
	}
	
	$this->assign('view_menu',  $_SESSION['wx_button']);
		$this->display('index.html');
	
	}
	function new_main_menu()
	{
		$menu=$_POST['menu'];
		$result=array('error'=>'0','data'=>'');
		if(isset($_SESSION['wx_button']))
		{
			if($_POST['menu']['type']==1)
			{
				$temp=array("type"=>"view","name"=>"$menu[name]","url"=>"$menu[url]");
			}
			else 
			{
				$temp=array("type"=>"click","name"=>"$menu[name]","key"=>"$menu[rule]");
			}
			$_SESSION['wx_button'][]=$temp;
		}
		print_r($result);
		
		
	}
	function new_sub_menu()
	{
		$menu=$_POST['menu'];
		$id=intval($menu['id']);
		$result=array('error'=>'0','data'=>'');
		if(isset($_SESSION['wx_button']))
		{
			if($_POST['menu']['type']==1)
			{
				$temp=array("type"=>"view","name"=>"$menu[name]","url"=>"$menu[url]");
			}
			else
			{
				$temp=array("type"=>"click","name"=>"$menu[name]","key"=>"$menu[rule]");
			}
			$_SESSION['wx_button'][$id]['sub_button'][]=$temp;
		}
		print_r($result);
	}
	function add_sub_menu()
	{
		$result=array('error'=>'0','data'=>'');
		
		if(isset($_SESSION['wx_button']))
		{
			if(count($_SESSION['wx_button'][$id]['sub_button'])>4)
			{
				$result['error']=1;
				die(json_encode($result));
			}
			
			
			
		}
		//获取规则列表
		$rule_list=Model('wxrule')->get_rule_list();
		if($rule_list)
		{
			foreach( $rule_list as $k=>$v)
			{
					
				$temp_list.="<option value='".$v['id']."'>".$v['name']."</option>";
					
			}
		}
		$result['data']=$temp_list;
		die(json_encode($result));
	
	}
	function add_main_menu()
	{
		$result=array('error'=>'0','data'=>'');
		if(isset($_SESSION['wx_button']))
		{
		   if(count($_SESSION['wx_button'])>2)
		   {
		   	  $result['error']=1;
		   	  die(json_encode($result));
		   }		
		}
		//获取规则列表
		$rule_list=Model('wxrule')->get_rule_list();
		if($rule_list)
		{
			foreach( $rule_list as $k=>$v)
			{
					
				$temp_list.="<option value='".$v['id']."'>".$v['name']."</option>";
					
			}
		}
		$result['data']=$temp_list;
		die(json_encode($result));
		
	}
	public function del_btn()
	{
		$id=isset($_GET['id'])?intval($_GET['id']):0;
		$result=array('error'=>1);
		
		if(isset($_SESSION['wx_button']))
		{
				
			foreach($_SESSION['wx_button'] as $k=>$v)
			{
				if($k==$id)
				{
					unset($_SESSION['wx_button'][$k]);
					$result['error']=0;
				}
				if($v['sub_button'])
				{
					foreach($v['sub_button'] as $kk=>$vv)
					{
		
						if($kk==$id)
						{
							
							unset($_SESSION['wx_button'][$k]['sub_button'][$kk]);
							$result['error']=0;
						}
					}
				}
			}
		}
		
		die(json_encode($result));
	}
	public function del_menu()
	{
		$app=Model('wxmenu')->get_app();
		$this->weiXinObj->getToken($app['appid'], $app['appsecret']);
		$result=json_decode($this->weiXinObj->del_menu());
		$this->showMsg('删除成功');
	}
	public function refresh_menu()
	{
		$result=array('error'=>1,'conent'=>'');
		if(isset($_SESSION['wx_button']))
		{
			unset($_SESSION['wx_button']);
			$result['error']=0;
		}
		$this->showMsg('刷新成功');
	}
	public function update_menu()
	{
		$menu=isset($_POST['menu'])?$_POST['menu']:'';
		$result=array('error'=>1,'conent'=>'');
		if($menu && $_SESSION['wx_button'])
		{
            $id=$menu['id'];
			foreach($_SESSION['wx_button'] as $k=>$v)
			{
				if($k==$id)
				{
					
					$result['error']=0;
					if($v['sub_button'])
					{
						$_SESSION['wx_button'][$k]['name']=$menu[name];
					}
					else 
					{
						if($menu['type']==1)
						{
							$temp=array("type"=>"view","name"=>"$menu[name]","url"=>"$menu[url]");
						}
						else
						{
							$temp=array("type"=>"click","name"=>"$menu[name]","key"=>"$menu[rule]");
						}
						$_SESSION['wx_button'][$k]=$temp;
					}
				}
				if($v['sub_button'])
				{
					foreach($v['sub_button'] as $kk=>$vv)
					{
			
						if($kk==$id)
						{
							$result['error']=0;
							if($menu['type']==1)
			               {
				                  $temp=array("type"=>"view","name"=>"$menu[name]","url"=>"$menu[url]");
			               }
			               else
			               {
			                  	$temp=array("type"=>"click","name"=>"$menu[name]","key"=>"$menu[rule]");
			                }
			                    $_SESSION['wx_button'][$k]['sub_button'][$kk]=$temp;
						}
					}
				}
			}
		}
		print_r(json_encode($result));
	}
	public function edit_menu()
	{
		$id=isset($_POST['id'])?intval($_POST['id']):0;
		$result=array('error'=>1,'data'=>'','list'=>'');
		$menu_list=Model('wxrule')->get_rule_list();
		$temp_list='';
	
		
		if(isset($_SESSION['wx_button']))
		{
			
			foreach($_SESSION['wx_button'] as $k=>$v)
			{
				if($k==$id)
				{
					$result['error']=0;
					$result['data']=$v;
				}
				if($v['sub_button'])
				{
					foreach($v['sub_button'] as $kk=>$vv)
					{
						
						if($kk==$id)
						{
							
							$result['error']=0;
							$result['data']=$vv;
						}
					}
				}
			}
		}
		
		if($menu_list)
		{
			foreach( $menu_list as $k=>$v)
			{
				if($v['id']==$result['data']['key'])
				{
					$temp_list.="<option selected='selected' value='".$v['id']."'>".$v['name']."</option>";
				}
				else 
				{
				$temp_list.="<option value='".$v['id']."'>".$v['name']."</option>";
				}
			}
		}
		$result['list']=$temp_list;
		print_r(json_encode($result));
		
	}
	public function recover_menu()
	{
		$menu=Model('wxmenu')->getCfg('wxmenu');
		$_SESSION['wx_button']=$menu;
		if($menu)
		{
			$this->showMsg(Lang('VALID_OPERATION'));
		}
		else
		{
			$this->showMsg(Lang('INVALID_OPERATION'));
		}
	}
	public function sumbit_menu()
	{
		$wx=new weixinController();
		$wxmu=Model('wxmenu');
		$app=$wxmu->get_app();
		$wx->getToken($app['appid'], $app['appsecret']);
		$button=array();
		$data=array();
		$json_result=array('error'=>'0','content'=>'');
		$temp=$_SESSION['wx_button'];
		foreach($temp as $k=>$v)
		{
			$temp[$k]['name']=urlencode($v['name']);
			if($v['key'])
			{
				$temp[$k]['key']="event_".$v['key'];
			}
			if($v['sub_button'])
			{
				unset($temp[$k]['sub_button']);
				foreach($v['sub_button'] as $kk=>$vv)
				{
					$vv['name']=urlencode($vv['name']);
					if($vv['key'])
					{
						$vv['key']="event_".$vv['key'];
					}
					$temp[$k]['sub_button'][]=$vv;
					//$temp[$k]['sub_button'][$kk]['name']=urlencode($vv['name']);
				}
			}
		}
		
		$button['button']=$temp;
		$result=json_decode($this->weiXinObj->create_menu(urldecode(json_encode($button))));
		if($result->errcode!=0)
		{
			$this->showMsg($result->errmsg);
		}
		else 
		{
			$this->showMsg($result->errmsg);
		}
		
		
	}
	public function save_menu()
	{
		$wx=new weixinController();
		$wxmu=Model('wxmenu');
		$app=$wxmu->get_app();
		$wx->getToken($app['appid'], $app['appsecret']);
		
		$button=array();
		$data=array();
		$json_result=array('error'=>'0','content'=>'');
		
		
	
		$button=$this->format_menu($_SESSION['wx_button']);
		$result=$wxmu->saveCfg('wxmenu',$button);
		if($result)
		{
			$this->showMsg(Lang('VALID_OPERATION'));
		}
		else
		{
			$this->showMsg(Lang('INVALID_OPERATION'));
		}
	
	
	}
	private function format_menu($temp)
	{
		$button=array();
		foreach($temp as $k=>$v)
		{
			$temp[$k]['name']=urlencode($v['name']);
			if($v['key'])
			{
				$temp[$k]['key']="event_".$v['key'];
			}
			if($v['sub_button'])
			{
				unset($temp[$k]['sub_button']);
				foreach($v['sub_button'] as $kk=>$vv)
				{
					$vv['name']=urlencode($vv['name']);
					if($vv['key'])
					{
						$vv['key']="event_".$vv['key'];
					}
					$temp[$k]['sub_button'][]=$vv;
					//$temp[$k]['sub_button'][$kk]['name']=urlencode($vv['name']);
				}
			}
		}
		
		$button['button']=$temp;
		return urldecode(json_encode($button));
	}
	
	
}